home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / finds101.zip / FINDSTR.DOC < prev    next >
Text File  |  1986-08-05  |  8KB  |  193 lines

  1.       FINDSTR.ARC - Find Multiple Strings in Multiple Files
  2.  
  3.  
  4. Version 1.01:   August 5, 1986
  5. Author:         Don A. Williams
  6. Language:       Datalight C Developer's Kit, Version 2.03
  7.  
  8.  
  9. FINDSTR  is  a  program that can be used to search multiple files 
  10. for multiple text strings.  It was actually developed as  a  test 
  11. program  for  a Boyer-Moore string search subroutine.  The Boyer-
  12. Moore algorithm is many times faster than the more common  string 
  13. search  algorithms and FINDSTR is several times faster than other 
  14. similar  programs.  It  does  not  provide  the  complex  pattern 
  15. matching  and  action statements of BAWK but is from 3 to 5 times 
  16. faster than BAWK at finding simple strings.  
  17.  
  18.  
  19. USAGE:
  20.  
  21.     A>FINDSTR [-c] file_spec [file_spec .....]
  22.  
  23. FINDSTR defaults to a case insensitive compare on the  assumption 
  24. that  is  better  to find too many occurrences than to miss some.  
  25. The '-C' command option  will  change  this  default  to  a  case 
  26. sensitive  compare  if that is what is required.  The '-C' option 
  27. can actually appear anywhere on the  command  line,  however,  no 
  28. file name specification may begin with a '-' as a result.  
  29.  
  30. FINDSTR can accept multiple file specifications  on  the  command 
  31. line  and  each file specification may be a full MS-DOS path name 
  32. which,  complements of Datalight C,  may  contain  "wild  cards".  
  33. FINDSTR  will read STDIN for the strings to be searched for.  The 
  34. search strings are entered one per line and may  contain  blanks, 
  35. quotes,  and  other  "special"  characters  but  may  not contain 
  36. carriage returns or line feeds (in this  version).  FINDSTR  will 
  37. quit  reading  search  strings  when  it encounters either a null 
  38. line, i.e. a line containing only a carriage return or an End-of-
  39. File (Control-Z).  Since STDIN may  be  redirected,  FINDSTR  can 
  40. read the search strings from either the console or a file.  
  41.  
  42.  
  43. OUTPUT:
  44.  
  45. For  each  file  that  it  processes,  FINDSTR will output a line 
  46. containing the path name of the file followed by  line  for  each 
  47. occurrence of one of the search strings in the file.  These lines 
  48. will contain the search string found followed by a blank followed 
  49. by  the  4-digit  decimal  line  number  of  the line in the file 
  50. followed by a ':' followed  by  a  blank  followed  by  the  line 
  51. itself.  
  52.  
  53.  
  54. EXAMPLES:
  55.  
  56. Since  the  output  lines  of  FINDSTR will frequently exceed the 
  57. length of the lines in this document,  they will be truncated  in 
  58. these examples; a '>' character in the right margin will indicate 
  59. that the line has been truncated.  
  60.  
  61. Example 1 - Search this document (to this point) for "the"
  62.  
  63.  
  64. C>FINDSTR findstr.doc
  65. FINDSTR Version 1.01: August 5, 1986
  66.  
  67. Enter search patterns, 1 per line
  68. NULL line terminates
  69. the
  70.  
  71. --findstr.doc--
  72. the   12: Moore algorithm is many times faster than the more com>
  73. the   13: search  algorithms and FINDSTR is several times faster>
  74. the   14: similar  programs.  It  does  not  provide  the  compl>
  75. the   23: FINDSTR can accept multiple file specifications  on  t>
  76. the   26: FINDSTR  will read STDIN for the strings to be searche>
  77. the   28: quotes,  and  other  "special"  characters  but  may  >
  78. the   30: quit  reading  search  strings  when  it encounters ei>
  79. the   33: read the search strings from either the console or a f>
  80. the   39: containing the path name of the file followed by  line>
  81. the   40: occurrence of one of the search strings in the file.  >
  82. the   41: will contain the search string found followed by a bla>
  83. the   42: by  the  4-digit  decimal  line  number  of  the line >
  84. the   43: followed by a ':' followed  by  a  blank  followed  by>
  85. the   49: Since  the  output  lines  of  FINDSTR will frequently>
  86. the   50: length of the lines in this document,  they will be tr>
  87. the   51: these examples; a '>' character in the right margin wi>
  88. the   52: that the line has been truncated.  
  89. the   54: Example 1 - Search this document (to this point) for ">
  90.                                                                  
  91.  
  92. Example 2 - Search the C source files for this program for "if" 
  93.             and "for".
  94.  
  95. C>FINDSTR *.c
  96. FINDSTR Version 1.01: August 5, 1986
  97.  
  98. Enter search patterns, 1 per line
  99. NULL line terminates
  100. if
  101. for
  102.  
  103. --BM.C--
  104. for    7:     for (i=0; i<256; i++) d[i] = pl;
  105. for    8:     for (i=0; i<pl-2; i++) d[Pattern[i]] = pl - i - 1;
  106. if   28:     if (j < 0) return(io - pl);
  107. --BMSEAR.C--
  108. for    7:       for (i=0; i<256; i++) d[i] = pl;
  109. for    8:       for (i=0; i<pl-1; i++) {
  110. if   30:        if (j < 0) return(io - pl);
  111. --FINDSTR.C--
  112. if   35:                if (pl == 0) break;
  113. if   37:                if (t == NULL) {
  114. for   38:                   fprintf(stderr, "Insufficient memory>
  115. if   42:                if (t->Pattern == 0) {
  116. for   43:                   fprintf(stderr, "Insufficient memory>
  117. if   47:                if (t->d == 0) {
  118. for   48:                       fprintf(stderr, "Insufficient me>
  119. if   54:                if (PatQueue.Head == NULL) PatQueue.Head>
  120. for   58:       for (fp=1; fp<argc; fp++) {
  121. if   59:                if ((F1 = fopen(argv[fp], "r")) == 0) {
  122. for   69:                       for (t=PatQueue.Head; t != NULL;>
  123. if   70:                                if ((p = BMSearch(Line, >
  124.  
  125. This example could also have been run by  creating  a  file,  say 
  126. INPUT,  containing  the  search strings,  one string to a line as 
  127. follows: 
  128.  
  129.     if
  130.     for
  131.  
  132. The command line would then be:
  133.  
  134. C>FINDSTR *.c <input
  135.  
  136. The output would be the same as shown  above.  The  output  could 
  137. also have been redirected to a file by the command line: 
  138.  
  139. C>FINDSTR *.C <input >output
  140.  
  141. In this case, all of the output from the first file name on would 
  142. have been put in the file,  OUTPUT.  Input and output redirection 
  143. are independent, i.e. either may be used without the other.  
  144.  
  145.  
  146. TECHNICAL CONSIDERATIONS:
  147.  
  148. The "heart" of this program  is  the  Boyer-Moore  string  search 
  149. algorithm.  This  algorithm  is the fastest known on the average.  
  150. The description of how  it  works  is  somewhat  complex  and  is 
  151. presented  in  "Data Structures and Algorithms" by Niklaus Wirth, 
  152. pages 66-69 and by the inventors R. S. Boyer and J.  S.  Moore in 
  153. "A Fast String Matching Algorithm",  Communications of  the  ACM, 
  154. 20,  10,  (Oct.  1977),  pp 762-772.  The algorithm does required 
  155. "compilation" of  each  search  string  and  is  best  suited  to 
  156. conditions  where  the  data  to be search is considerably larger 
  157. than the search string.  The routine,  BMCompile,  in the module, 
  158. BMSEAR,  performs the compilation and the routine,  BMSearch,  in 
  159. the same module performs  the  actual  search.  For  each  search 
  160. string,  the  algorithm  requires  a  integer  array  to hold the 
  161. "compiled" string as well as space to retain the  string  itself.  
  162. I  have  chosen  to  make  these arrays 256 entries long to allow 
  163. strings to contain characters above the standard ASCII 128.  
  164.  
  165. FINDSTR forms the input search strings into  a  simple  First-In-
  166. First-Out  (FIFO)  linked  list,  acquiring  memory  for both the 
  167. string itself and for its "compiled" array dynamically.  
  168.  
  169. The expansion of "wild card" file specifications on  the  command 
  170. line  is  performed  by  a  proprietary  module supplied with the 
  171. Datalight C compiler,  however,  other good C  compilers  provide 
  172. similar  facilities.   Outside  of  the  "wild  card"  expansion, 
  173. FINDSTR and BMSEAR are very "standard" C and should be compilable 
  174. by any other C compiler.  
  175.  
  176. A similar Boyer-Moore search algorithm is also available in Turbo 
  177. Pascal.  
  178.  
  179.  
  180. COPYRIGHT CONSIDERATIONS:
  181.  
  182. As far as I can determine,  everything  in  FINDSTR  and  BMSEAR, 
  183. except  the expansion of "wild card" command line parameters,  is 
  184. in the public domain and no copyright restrictions  apply.  Since 
  185. the   total  development  time  for  this  program  from  initial 
  186. conception to the production of this document was under 9  hours, 
  187. the  usual  request  for  "donations" is absurd.  The author,  of 
  188. course,   disclaims  liability  for  any  damages  of  any   kind 
  189. whatsoever